/*
 * task_subject
 * task_priority
 * task_complete
 * task_dueat
 * task_dueat_text
 * task_startdate
 * task_tags
 * task_members
 * task_contents 
 */

var task_subject_state = 1;

var currentTask = dummyTask;

function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

function onblurHandler(id) {
	switch (id) {
	   case 'task_subject':
		   currentTask.title = document.forms[0].elements[0].value; 
		   break;
	   case 'task_priority':
		   currentTask.priority = document.forms[0].elements[0].value;
		   break;
	   case 'task_complete':
		   currentTask.progress = document.forms[0].elements[0].value;
		   break;
	   case 'task_dueat':
		   currentTask.dueDate = document.forms[0].elements[0].value;
		   break;
	   case 'task_startdate':
		   currentTask.addedDate = document.forms[0].elements[0].value;
		   break;
	   case 'task_tags':
		   currentTask.tags = document.forms[0].elements[0].value;
		   break;
	   case 'task_members':
		   currentTask.members = document.forms[0].elements[0].value;
		   break;
	   case 'task_contents':
		   currentTask.contents = document.forms[0].elements[0].value;
		   break;
	   // default: TBD;
	}
	document.getElementById(id).innerHTML = document.forms[0].elements[0].value;
	task_subject_state = 1;
}

function clickHandler(id) {
	var input_field_fhtml;
	
	switch (id) {
	   case 'task_subject':
		   input_field_fhtml = '<INPUT TYPE="text" ID="task_inputbox" NAME="task_subject_inputbox" OnKeyPress="return disableEnterKey(event)" VALUE="' + currentTask.title + '">'; 
		   break;
	   case 'task_priority':
		   input_field_fhtml = '<INPUT TYPE="text" ID="task_inputbox" NAME="task_subject_inputbox" OnKeyPress="return disableEnterKey(event)" VALUE="' + currentTask.priority + '">';
		   break;
	   case 'task_complete':
		   input_field_fhtml = '<INPUT TYPE="text" ID="task_inputbox" NAME="task_subject_inputbox" OnKeyPress="return disableEnterKey(event)" VALUE="' + currentTask.progress + '">';
		   break;
	   case 'task_dueat':
		   input_field_fhtml = '<INPUT TYPE="text" ID="task_inputbox" NAME="task_subject_inputbox" OnKeyPress="return disableEnterKey(event)" VALUE="' + currentTask.dueDate + '">';
		   break;
	   case 'task_startdate':
		   input_field_fhtml = '<INPUT TYPE="text" ID="task_inputbox" NAME="task_subject_inputbox" OnKeyPress="return disableEnterKey(event)" VALUE="' + currentTask.addedDate + '">';
		   break;
	   case 'task_tags':
		   input_field_fhtml = '<INPUT TYPE="text" ID="task_inputbox" NAME="task_subject_inputbox" OnKeyPress="return disableEnterKey(event)" VALUE="' + currentTask.tags + '">';
		   break;
	   case 'task_members':
		   input_field_fhtml = '<INPUT TYPE="text" ID="task_inputbox" NAME="task_subject_inputbox" OnKeyPress="return disableEnterKey(event)" VALUE="' + currentTask.members + '">';
		   break;
	   case 'task_contents':
		   input_field_fhtml = '<INPUT TYPE="text" ID="task_inputbox" NAME="task_subject_inputbox" OnKeyPress="return disableEnterKey(event)" VALUE="' + currentTask.contents + '">';
		   break;
	   // default: TBD;
	}
	
	if (task_subject_state === 1) // guards against double activation
	{
		task_subject_state = 0;
		document.getElementById(id).innerHTML = input_field_fhtml;
		document.getElementById('task_inputbox').onblur = function () { onblurHandler(id); };
		document.getElementById('task_inputbox').focus();
	}
}

function init_from_current_task()
{
	// currently we're going to use dummy task for this
	document.getElementById('task_subject').innerHTML = currentTask.title;
	document.getElementById('task_priority').innerHTML = currentTask.priority;
	document.getElementById('task_complete').innerHTML = currentTask.progress;
	document.getElementById('task_dueat').innerHTML = currentTask.dueDate;
	document.getElementById('task_dueat_text').innerHTML = "(3 days from now (FIXME))"; 
	document.getElementById('task_startdate').innerHTML = currentTask.addedDate;
	document.getElementById('task_tags').innerHTML = currentTask.tags;
	document.getElementById('task_members').innerHTML = currentTask.owner;
	//document.getElementById('task_contents ').innerHTML = currentTask.Value;
}

function start() {
	// the start function will first initialize all onclick events
	// for all objects who need an onclick.
	  document.getElementById('task_subject').onclick = function () { clickHandler('task_subject'); };
	  document.getElementById('task_subject_hdr').onclick = function () { clickHandler('task_subject'); };
	  document.getElementById('task_priority').onclick = function () { clickHandler('task_priority'); };
	  //document.getElementById('task_priority_hdr').onclick = function () { clickHandler('task_priority'); };
	  document.getElementById('task_complete').onclick = function () { clickHandler('task_complete'); };
	  //document.getElementById('task_complete_hdr').onclick = function () { clickHandler('task_complete'); };
	  document.getElementById('task_dueat').onclick = function () { clickHandler('task_dueat'); };
	  document.getElementById('task_dueat_hdr').onclick = function () { clickHandler('task_dueat'); };
	  document.getElementById('task_startdate').onclick = function () { clickHandler('task_startdate'); };
	  document.getElementById('task_startdate_hdr').onclick = function () { clickHandler('task_startdate'); };
	  document.getElementById('task_tags').onclick = function () { clickHandler('task_tags'); };
	  document.getElementById('task_tags_hdr').onclick = function () { clickHandler('task_tags'); };
	  document.getElementById('task_members').onclick = function () { clickHandler('task_members'); };
	  document.getElementById('task_members_hdr').onclick = function () { clickHandler('task_members'); };
	  document.getElementById('task_contents').onclick = function () { clickHandler('task_contents'); };
	  document.getElementById('task_contents_hdr').onclick = function () { clickHandler('task_contents'); };
	  init_from_current_task();
	}

window.onload = start;
